package com.ssyt.store.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssyt.store.common.BaseController;
import com.ssyt.store.common.util.StringUtil;
import com.ssyt.store.sys.entity.StoreOrder;
import com.ssyt.store.sys.entity.StoreOrderProduct;
import com.ssyt.store.sys.entity.StoreTicket;
import com.ssyt.store.sys.entity.SysUserEntity;
import com.ssyt.store.sys.service.StoreOrderProductService;
import com.ssyt.store.sys.service.StoreOrderService;
import com.ssyt.store.sys.service.StoreReceiveAddressService;
import com.ssyt.store.sys.service.StoreTicketService;

@Controller
@RequestMapping("/sys/order")
public class OrderController extends BaseController {

	@Autowired
	private StoreOrderProductService storeOrderProductService;
	@Autowired
	private StoreOrderService storeOrderService;
	@Autowired
	private StoreTicketService storeTicketService;
	
	
	
	@RequestMapping("/listAllStoreOrder")
	@ResponseBody
	public List<StoreOrder> listAllStoreOrder(int page, int rows){
		List<StoreOrder> orders = storeOrderService.listAllStoreOrder();
		if(orders != null){
			for (StoreOrder storeOrder : orders) {
				String ticketId = storeOrder.getTicket();
				if(StringUtil.isNotEmpty(ticketId)){
					StoreTicket ticket = storeTicketService.getStoreTicket(ticketId);
					storeOrder.setStoreTicket(ticket);
				}
				
			}
		}
		
		return orders;
	}
	
	@RequestMapping("/getOrderProducts")
	@ResponseBody
	public List<StoreOrderProduct>  getOrderProducts(String storeProductsIdsStr){
		List<StoreOrderProduct> storeOrderProducts = new ArrayList<StoreOrderProduct>();
		String[] storeProductsIds = storeProductsIdsStr.split(",");
		for (int i = 0; i < storeProductsIds.length; i++) {
			StoreOrderProduct storeOrderProduct =storeOrderProductService.getStoreOrderProduct(storeProductsIds[i]);
			storeOrderProducts.add(storeOrderProduct);
		}
		return storeOrderProducts;
	}
	
	
	@RequestMapping("/sendProducts")
	@ResponseBody
	public void sendProducts(String orderId){
		storeOrderService.sendProduct(orderId);
	}
	
	
	@RequestMapping("/deleteOrder")
	@ResponseBody
	public void deleteOrder(String orderId){
		storeOrderService.deleteStoreOrder(orderId);
	}
}
